<?php
/**
 * Created by PhpStorm.
 * User: bill
 * Date: 2014/12/2
 * Time: 10:10
 */

include_once __DIR__ . '/bs.php';
//处理所有二维码类
class C二维码 extends CBS
{


    function __construct(){
        parent::__construct();
    }


    //扫码
    public static function f扫码($qrcode)
    {

    }

//todo 多收银台11
    public static  function fz增加二维码(Do用户 $vo用户,$备注,$type,$二维码类型,$vi公众号ID,$vi公众号openID,$定制code=null,$时间 ='1800')
    {

        if($二维码类型==1){
            if($定制code){
                $codeId=$定制code;
            }else{
                if($type == 5  ){
                    $codeId = $备注.'qun'.self::get永久二维码编号($vo用户->get_id());//生成二维码规则
                }else{
                    $codeId = self::get永久二维码编号($vo用户->get_id());//生成二维码规则
                }

            }

            $do = new DoQRcode();
            $fname=$codeId;
            $rets = C信息中心::get实列()->vc公众号->fc创建字符二维码($codeId, $fname);
           // print_r($rets);
        }else{
            if($type == 5  ){
                $codeId = $备注.self::get临时二维码编号($vo用户->get_id());//生成二维码规则
            }else{
                $codeId = self::get临时二维码编号($vo用户->get_id());//生成二维码规则
            }


            $do = new DoQRcode();
            $fname=$codeId;
            $rets =  C信息中心::get实列()->vc公众号->fc创建二维码($codeId,$fname,false,$时间);
        }

        $msg = $rets['message'];
        $do->set_id(null);
        $do->set_serv_id(wzd()->serv_id);
        $do->set_公众号ID($vi公众号ID);
        $do->set_公众号openID($vi公众号openID);
        $do->set_status(1);
        $do->set_code($codeId);
        $do->set_ticket($msg->ticket);
        if(isset($msg->expire_seconds))
            $do->set_expire_time($msg->expire_seconds);
        $do->set_url($msg->url);

        $do->set_image_file(Constants::e二维码_存放目录 . $fname . '.jpg');
        $do->set_type(1);
        $do->set_user_id($vo用户->get_id());
        $do->set_user_open_id($vo用户->get_openID());
        $do->set_business($type);
        $do->set_note($备注);

        logger()->logInfo("创建二维码：".var_export($do,true));

        $dbc = $do->insert();
        logger()->logDebug2($dbc,"保存数据记录结果:");
        if ($dbc) {
            return $codeId;

        } else {
            return false;
        }



    }


    /**
     * 创建二维码
     * @param Do用户 $vo用户
     * @param bool $permanent
     * @return bool|DoQRcode
     */
    public static function f创建(Do用户 $vo用户, $permanent = false,$vi公众号ID,$vi公众号openID)
    {

        if ($permanent) {
            $codeId = self::get永久二维码编号($vo用户->get_id());
        } else {
            $codeId = self::get临时二维码编号($vo用户->get_id());
        }
        $do = new DoQRcode();

        if ($permanent) {
            $fname = Constants::e二维码_存放目录 . $codeId . '.jpg';
        } else {
            $fname = Constants::e二维码_存放目录 . $codeId . '.jpg';
        }
        $rets =  C信息中心::get实列()->vc公众号->fc创建字符二维码($codeId, $codeId, $permanent);
        if (!$rets) {
            return false;
        }
        $msg = $rets['message'];
        //return  $msg; die;
        $do->set_serv_id(wzd()->serv_id);
        $do->set_公众号ID($vi公众号ID);
        $do->set_公众号openID($vi公众号openID);
        $do->set_status(1);
        $do->set_code($codeId);
        $do->set_ticket($msg->ticket);
        if(isset($msg->expire_seconds))
            $do->set_expire_time($msg->expire_seconds);
        $do->set_url($msg->url);
        $do->set_image_file($fname);
        $do->set_type($permanent ? 1 : 0);
        $do->set_business(1);
        $do->set_user_id($vo用户->get_id());
        $do->set_user_open_id($vo用户->get_openID());
        logger()->logInfo("创建二维码：".var_export($do,true));
        $dbc = $do->save();

        logger()->logDebug2($dbc,"保存数据记录结果:");
        if ($dbc) {
            return $do;
        } else {
            return false;
        }

    }

    //todo 多收银台11
    private static function get永久二维码编号($vi用户id){
        //如果是永久二维码，则二维码编号由系统分配，总数不超过100000
        //目前用户数目不超过10000, 则永久二维码编号就是用户id
        if($vi用户id<Constants::permQRNumber) return $vi用户id.date('YmdHis',time());
        return false;
    }
    //todo 多收银台11
    //如果是用户创建的临时二维码，则二维码的id就是用户id + 100000
    private static function get临时二维码编号($vi用户id){
        return  Constants::permQRNumber +$vi用户id.date('YmdHis',time());

    }
    public static function f查询($用户id,$permanent=false){
        //TODO 获取永久二维码id的方式需要变更，目前取100000以内的用户id作为永久二维码id
       /* if($permanent)
            $codeId=$vi二维码编号;
        else
            $codeId=Constants::permQRNumber+$vi二维码编号;
*/
        $pdo=pdo();
        $do=new DoQRcode();
        $cou=$pdo->selectBO($do,array("user_id"=>$用户id,"business"=>'1'));
        /*
       $do->set_serv_id(wzd()->serv_id);
        $do->user_id($用户id);
        $do->set_status(1);
        $do->set_business(1);
        $do->set_type($permanent?1:0);
        $do->set_business(1);
       // $do->set_code($codeId);

        $dbc=$do->loadByKeys($do->get属性数组());

        if($dbc)
            return $do;
        else
            return false;
*/
        if($cou)
            return $cou;
        else
            return false;
    }

    public static function f二维码所属用户($vi二维码编号,$permanent=false){
//        if($permanent)
//            $vi用户id=$vi二维码编号;
//        else
//            $vi用户id=$vi二维码编号-Constants::permQRNumber;
//        return $vi用户id;
        $pdo=pdo();
        $do=new DoQRcode();
        $cou=$pdo->selectBO($do,array("code"=>$vi二维码编号));

        if($cou)
            return $cou->data['user_id'];
        else
            return false;
    }

    public static function f二维码是否属于用户推广($vi二维码编号,$permanent=false){
        $pdo=pdo();
        $do=new DoQRcode();
        $cou=$pdo->selectBO($do,array("code"=>$vi二维码编号,"note"=>"用户推广二维码"));

        if($cou)
            return "用户推广二维码";
        else
            return false;
    }
    public static function getQRcodeByUrl($url){
        if(!isset($url)) return false;
        //$ret=Linhe::DB()->select("SELECT `code` FROM `qrcode` where `url`='{$url}'");
        $do=new DoQrcode();
        $ret=self::$pdo->selectColumns($do->tableName(),[$do->get_code(false)],[$do->get_url(false)=>$url]);

        if($ret)
            return $ret[$do->get_code(false)];
        else
            return false;
    }



    private function getMaxPermamentQRcode()
    {
        $sql = "SELECT max(`code`) FROM `qrcode` WHERE type=1";

        $ret = self::$pdo->selectOne($sql);

        return $ret;
    }

}